<?php
/**
 * 用户管理
 * Date: 2015/9/8
 * Time: 15:34
 */
namespace AdminV3\Controller;
use Think\Controller;

class UserController extends CommonController
{
    //用户显示
    public function index()
    {
        $userList = D('UserRoleView')->select();
        $this->assign('userlist',$userList);
        $this->display();
    }

    //增加,修改用户
    public function addUser()
    {
        if(isset($_GET['uid'])){
            $userData = D('UserRoleView')->where('user.id ='.$_GET['uid'])->select();
           // var_dump($userData);die();
            $this->assign('userData',$userData[0]);
        }

        $roleList = M('role')->where('status=1')->field('id,name')->select();
        $this->assign('roleList',$roleList);
        $this->display();
    }

    // 增加存入数据库
    public function insertUser()
    {
        $username =trim(I('post.username'));
        $name =trim(I('post.name'));
        $paw =trim(I('post.paw'));
        $repaw =trim(I('post.repaw'));
        $remark =trim(I('post.remark'));
        $role =trim(I('post.role'));
        $status =I('post.status');

       if(!empty($username) && !empty($name) && !empty($paw) && !empty($repaw) && !empty($role) ){

           if( $paw != $repaw){
               $this->error('两次密码输入不对！');
           }
           $userModel = M('user');
           $id = $userModel->where('username="'.$username.'"')->field('id')->select();
           if(!empty($id)){
               $this->error('账号已经存在！');
           }

           $data['username'] = $username;
           $data['name'] =$name;
           $data['password'] =md5($paw);
           $data['status'] = $status;
           $data['remark'] = $remark;
           $data['reg_date'] = date('Y-m-d H:i:s',time());
           $data['last_login_date'] =date('Y-m-d H:i:s',time());

           $userModel->startTrans();
           $re =$userModel->add($data);
           if($re>0){
               $roleUserData['role_id'] = $role;
               $roleUserData['user_id'] = $userModel->getLastInsID();
               if(M('roleUser')->add($roleUserData)>0){
                   $userModel->commit();
                   $this->redirect('User/index');
               }else{
                   $userModel->rollback();
                   $this->error('添加用户不成功！');
               }
           }else{
               $userModel->rollback();
               $this->error('添加用户不成功！');
           }

       }else{
           $this->error('数据不完整！');
       }

    }

    //修改保存到数据库
    public function saveUser()
    {
        $uid = trim(I('post.uid'));
        $name =trim(I('post.name'));
        $remark =trim(I('post.remark'));
        $role =trim(I('post.role'));
        $status =I('post.status');

        if(!empty($uid) &&  !empty($name)  && !empty($role) ){

            $userModel = M('user');
            $data['name'] =$name;
            $data['status'] = $status;
            $data['remark'] = $remark;
            $data['remark'] = $remark;

            $re =$userModel->where('id='.$uid)->save($data);

            $roleUserData['role_id'] = $role;

            $re2= M('RoleUser')->where('user_id='.$uid)->save($roleUserData);


                if($re ||  $re2){

                    $this->redirect('User/index');
                }else{
                    $userModel->rollback();
                    $this->error('添加用户不成功！');
                }



        }else{
            $this->error('数据不完整！');
        }

    }

    //修改账号的状态
    public function changStatus()
    {
        if(isset($_GET['uid']) && isset($_GET['st'])){

            $status = $_GET['st'] == 1?0:1;
            $db = M('user')->where('id='.I('get.uid'))->setField('status',$status);
            if($db > 0){
                $this->redirect('User/index');
            }else{
                $this->error('状态修改失败');
            }
        }else{
            $this->error('非法操作，请按正常流程来操作！');
        }


    }

    // admin 修改账号密码
    public function changPaw()
    {
        //判断是不是 admin 账号
        if(session('id') ==1){
            $uid = I('get.uid');
            $this->assign('uid',$uid);
            $this->display();
        }else{
            $this->error('你不是超级管理员，你不能修改其他账户的密码！');
        }

    }

    public function savePaw()
    {
        $uid = trim(I('post.uid'));
        if(!empty($uid)){
            $pw = trim(I('post.pw'));
            $repw = trim(I('post.repw'));

            if(!empty($pw) && !empty($repw) && ($pw == $repw)){

                $re = M('user')->where('id ='.$uid)->setField('password',md5($pw));
                if($re>0){
                    $this->redirect('User/index');
                }else{
                    $this->error('修改密码不成功！');
                }

            }else{
                $this->error('不能为空或 两次密码不一致！');
            }

        }else{
            $this->error('非法操作，请返回模块首页！');
        }

    }



}